/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.raflik.queryxml.serializer.standard;

import javax.swing.ListCellRenderer;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;
import org.w3c.dom.Attr;
import pl.raflik.queryxml.lib.ResultType;

final class QueryOutputSerializationPanel extends javax.swing.JPanel {

    private static final ListCellRenderer booleanRenderer = new BooleanCellRenderer();

    private final QueryOutputSerializationOptionsPanelController controller;
    private DocumentListener updatePreviewListener = new DocumentListener() {

        private void updatePreview() {
            generatedPreviewL.setText(generatePreview());
        }

        public void insertUpdate(DocumentEvent e) {
            updatePreview();
        }

        public void removeUpdate(DocumentEvent e) {
            updatePreview();
        }

        public void changedUpdate(DocumentEvent e) {
            updatePreview();
        }
    };
    private DocumentListener controllerChangedListener = new DocumentListener() {

        public void insertUpdate(DocumentEvent e) {
            controller.changed();
        }

        public void removeUpdate(DocumentEvent e) {
            controller.changed();
        }

        public void changedUpdate(DocumentEvent e) {
            controller.changed();
        }
    };

    QueryOutputSerializationPanel(QueryOutputSerializationOptionsPanelController controller) {
        this.controller = controller;
        initComponents();
        // TODO listen to changes in form fields and call controller.changed();
        Document doc1 = firstSeparatorTF.getDocument();
        doc1.addDocumentListener(updatePreviewListener);
        doc1.addDocumentListener(controllerChangedListener);
        Document doc2 = secondSeparatorTF.getDocument();
        doc2.addDocumentListener(updatePreviewListener);
        doc2.addDocumentListener(controllerChangedListener);
        resultRootTF.getDocument().addDocumentListener(controllerChangedListener);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        firstSeparatorL = new javax.swing.JLabel();
        secondSeparatorL = new javax.swing.JLabel();
        firstSeparatorTF = new javax.swing.JTextField();
        secondSeparatorTF = new javax.swing.JTextField();
        previewL = new javax.swing.JLabel();
        generatedPreviewL = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        resultRootL = new javax.swing.JLabel();
        resultRootTF = new javax.swing.JTextField();
        omitXmlDeclarationL = new javax.swing.JLabel();
        omitXmlDeclarationCB = new javax.swing.JComboBox();
        indentL = new javax.swing.JLabel();
        indentCB = new javax.swing.JComboBox();

        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.PAGE_AXIS));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(QueryOutputSerializationPanel.class, "TextTransformer.supportedOutputType"))); // NOI18N
        jPanel1.setPreferredSize(new java.awt.Dimension(590, 140));

        firstSeparatorL.setLabelFor(firstSeparatorTF);
        org.openide.awt.Mnemonics.setLocalizedText(firstSeparatorL, org.openide.util.NbBundle.getMessage(QueryOutputSerializationPanel.class, "QueryOutputSerializationPanel.firstSeparatorL.text")); // NOI18N

        secondSeparatorL.setLabelFor(secondSeparatorTF);
        org.openide.awt.Mnemonics.setLocalizedText(secondSeparatorL, org.openide.util.NbBundle.getMessage(QueryOutputSerializationPanel.class, "QueryOutputSerializationPanel.secondSeparatorL.text")); // NOI18N

        firstSeparatorTF.setColumns(1);
        firstSeparatorTF.setText(org.openide.util.NbBundle.getMessage(QueryOutputSerializationPanel.class, "QueryOutputSerializationPanel.firstSeparatorTF.text")); // NOI18N

        secondSeparatorTF.setColumns(1);
        secondSeparatorTF.setText(org.openide.util.NbBundle.getMessage(QueryOutputSerializationPanel.class, "QueryOutputSerializationPanel.secondSeparatorTF.text")); // NOI18N

        previewL.setLabelFor(generatedPreviewL);
        org.openide.awt.Mnemonics.setLocalizedText(previewL, org.openide.util.NbBundle.getMessage(QueryOutputSerializationPanel.class, "QueryOutputSerializationPanel.previewL.text")); // NOI18N

        generatedPreviewL.setFont(generatedPreviewL.getFont().deriveFont((generatedPreviewL.getFont().getStyle() | java.awt.Font.ITALIC)));
        generatedPreviewL.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        org.openide.awt.Mnemonics.setLocalizedText(generatedPreviewL, generatePreview());

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(firstSeparatorL)
                            .addComponent(secondSeparatorL))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(firstSeparatorTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(secondSeparatorTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(previewL)
                        .addGap(18, 18, 18)
                        .addComponent(generatedPreviewL)))
                .addContainerGap(405, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(firstSeparatorL)
                    .addComponent(firstSeparatorTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(7, 7, 7)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(secondSeparatorL)
                    .addComponent(secondSeparatorTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(previewL)
                    .addComponent(generatedPreviewL))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        add(jPanel1);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(QueryOutputSerializationPanel.class, "XMLTransformer.supportedOutputType"))); // NOI18N

        resultRootL.setLabelFor(resultRootTF);
        org.openide.awt.Mnemonics.setLocalizedText(resultRootL, org.openide.util.NbBundle.getMessage(QueryOutputSerializationPanel.class, "QueryOutputSerializationPanel.resultRootL.text")); // NOI18N

        resultRootTF.setColumns(6);
        resultRootTF.setText(org.openide.util.NbBundle.getMessage(QueryOutputSerializationPanel.class, "QueryOutputSerializationPanel.resultRootTF.text")); // NOI18N

        omitXmlDeclarationL.setLabelFor(omitXmlDeclarationCB);
        org.openide.awt.Mnemonics.setLocalizedText(omitXmlDeclarationL, org.openide.util.NbBundle.getMessage(QueryOutputSerializationPanel.class, "QueryOutputSerializationPanel.omitXmlDeclarationL.text")); // NOI18N

        omitXmlDeclarationCB.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "false", "true" }));
        omitXmlDeclarationCB.setRenderer(booleanRenderer);

        indentL.setLabelFor(indentCB);
        org.openide.awt.Mnemonics.setLocalizedText(indentL, org.openide.util.NbBundle.getMessage(QueryOutputSerializationPanel.class, "QueryOutputSerializationPanel.indentL.text")); // NOI18N

        indentCB.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "true", "false" }));
        indentCB.setRenderer(booleanRenderer);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(resultRootL)
                        .addGap(18, 18, 18)
                        .addComponent(resultRootTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(indentL)
                        .addGap(18, 18, 18)
                        .addComponent(indentCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(omitXmlDeclarationL)
                        .addGap(18, 18, 18)
                        .addComponent(omitXmlDeclarationCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(339, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(resultRootL)
                    .addComponent(resultRootTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(indentL)
                    .addComponent(indentCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(omitXmlDeclarationL)
                    .addComponent(omitXmlDeclarationCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(112, Short.MAX_VALUE))
        );

        add(jPanel2);
    }// </editor-fold>//GEN-END:initComponents

    void load() {
        // TODO read settings and initialize GUI
        // Example:        
        // someCheckBox.setSelected(Preferences.userNodeForPackage(QueryOutputSerializationPanel.class).getBoolean("someFlag", false));
        // or for org.openide.util with API spec. version >= 7.4:
        // someCheckBox.setSelected(NbPreferences.forModule(QueryOutputSerializationPanel.class).getBoolean("someFlag", false));
        // or:
        // someTextField.setText(SomeSystemOption.getDefault().getSomeStringProperty());
        firstSeparatorTF.setText(TextTransformer.getFirstSeparator());
        secondSeparatorTF.setText(TextTransformer.getSecondSeparator());
        
        resultRootTF.setText(XMLTransformer.getXMLResultRoot());
        indentCB.setSelectedItem(Boolean.toString(XMLTransformer.getIndent()));
        omitXmlDeclarationCB.setSelectedItem(Boolean.toString(XMLTransformer.getOmitXmlDeclaration()));

        generatedPreviewL.setText(generatePreview());
    }

    void store() {
        // TODO store modified settings
        // Example:
        // Preferences.userNodeForPackage(QueryOutputSerializationPanel.class).putBoolean("someFlag", someCheckBox.isSelected());
        // or for org.openide.util with API spec. version >= 7.4:
        // NbPreferences.forModule(QueryOutputSerializationPanel.class).putBoolean("someFlag", someCheckBox.isSelected());
        // or:
        // SomeSystemOption.getDefault().setSomeStringProperty(someTextField.getText());
        TextTransformer.setFirstSeparator(firstSeparatorTF.getText());
        TextTransformer.setSecondSeparator(secondSeparatorTF.getText());

        XMLTransformer.setXMLResultRoot(resultRootTF.getText());
        boolean indent = Boolean.parseBoolean(indentCB.getSelectedItem().toString());
        XMLTransformer.setIndent(indent);
        boolean omitXmlDeclaration = Boolean.parseBoolean(omitXmlDeclarationCB.getSelectedItem().toString());
        XMLTransformer.setOmitXmlDeclaration(omitXmlDeclaration);
    }

    private boolean emptyCheck(String value) {
        if (value == null || value.equals("")) {
            return true;
        }
        return false;
    }

    boolean valid() {
        // TODO check whether form is consistent and complete
        // TextTransformer
        String firstSeparator = firstSeparatorTF.getText();
        String secondSeparator = secondSeparatorTF.getText();
        if (emptyCheck(firstSeparator) || emptyCheck(secondSeparator)) {
            return false;
        }
        //XMLTransformer
        String xmlRoot = resultRootTF.getText();
        if (emptyCheck(xmlRoot)) {
            return false;
        }
        return true;
    }

    private String generatePreview() {
        return ResultType.formatType(Attr.class) + firstSeparatorTF.getText() + "attr" + secondSeparatorTF.getText() + "value";
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel firstSeparatorL;
    private javax.swing.JTextField firstSeparatorTF;
    private javax.swing.JLabel generatedPreviewL;
    private javax.swing.JComboBox indentCB;
    private javax.swing.JLabel indentL;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JComboBox omitXmlDeclarationCB;
    private javax.swing.JLabel omitXmlDeclarationL;
    private javax.swing.JLabel previewL;
    private javax.swing.JLabel resultRootL;
    private javax.swing.JTextField resultRootTF;
    private javax.swing.JLabel secondSeparatorL;
    private javax.swing.JTextField secondSeparatorTF;
    // End of variables declaration//GEN-END:variables
}
